//
//  Record.m
//  Estimation
//
//  Created by Safeneeds-MacBookPro on 2013/07/03.
//  Copyright (c) 2013年 topsign. All rights reserved.
//

#import "Record.h"
#import "RecordField.h"
#import "MainViewController.h"
#import "DBbase.h"

@implementation Record

@synthesize tableName;
@synthesize keyFields;
@synthesize fields;
@synthesize mode;

-(id)init
{
    if(self=[super init])
    {
        //custom
        self.keyFields =[[NSMutableDictionary alloc]init];
        self.fields =[[NSMutableDictionary alloc]init];
        self.mode = NEW;
    }
    return self;
}

-(id)initWithTableName:(NSString *) name
{
    if (self=[super init]) {
        //custom
        self.tableName = name;
        self.keyFields =[[NSMutableDictionary alloc]init];
        self.fields =[[NSMutableDictionary alloc]init];
        self.mode = NEW;
    }
    return self;
}

//- (void) dealloc{
//    [self.tableName release];
//    [self.keyFields release];
//    [self.fields release];
//    [super dealloc];
//}

-(int) doSave
{
    int success;
    if( self.mode == NEW){
        success = executeDbUpdate([self getInsertSql]);
        
    }else{
        success =  executeDbUpdate([self getUpdateSql]);
    }
    
    return success;
}

-(int) updateField:(NSString *)fieldName
{
    return executeDbUpdate([self getUpdateFieldSql:fieldName]);
}

- (int) doDel
{
    int success =  executeDbUpdate([self getDelSql]);
    
    return success;

}

-(int) loadFromDB
{
    char *querySql = [self getLoadSql];
    
    NSMutableArray *records = getDbRecords(querySql,self.tableName,[self.keyFields allKeys]);
    
    if([records count] > 0){
        Record *newRecord = [records firstObjectCommonWithArray:records];
        
        self.mode = newRecord.mode;
        self.keyFields=newRecord.keyFields;
        self.fields = newRecord.fields;
        
        return 1;
    }
    return 0;

}

-(char *)getInsertSql
{
    NSString *fieldNames =@"";
    NSString *values =@"";
    
    NSEnumerator * keyFieldKeys = [self.keyFields keyEnumerator];
    
    for (NSString *key in keyFieldKeys) {
        RecordField *field = [self.keyFields objectForKey: key];
        
        if(fieldNames.length==0){
            fieldNames = field.fieldName;
        }
        else{
            fieldNames = [fieldNames stringByAppendingString:@","];
            fieldNames = [fieldNames stringByAppendingString:field.fieldName];
        }
        
        
        if(values.length==0){
            values = [field getSQLValueStr];
        }
        else{
            values = [values stringByAppendingString:@","];
            values = [values stringByAppendingString:[field getSQLValueStr]];
        }
    }
    
    NSEnumerator * fieldKeys = [self.fields keyEnumerator];
    
    for (NSString *key in fieldKeys) {
        RecordField *field = [self.fields objectForKey: key];
        
        if(fieldNames.length==0){
            fieldNames = field.fieldName;
        }
        else{
            fieldNames = [fieldNames stringByAppendingString:@","];
            fieldNames = [fieldNames stringByAppendingString:field.fieldName];
        }
        
        
        if(values.length==0){
            values = [field getSQLValueStr];
        }
        else{
            values = [values stringByAppendingString:@","];
            values = [values stringByAppendingString:[field getSQLValueStr]];
        }
    }

    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO %@ (%@) VALUES (%@)",self.tableName,fieldNames,values];
    
    NSLog(@"%@",sql);

    return (char *)[sql UTF8String];
}
-(char *)getDelSql
{
    NSString *where =@"";
    
    NSEnumerator * keyFieldKeys = [self.keyFields keyEnumerator];
    
    for (NSString *key in keyFieldKeys) {
        RecordField *field = [self.keyFields objectForKey: key];
        
        if(where.length==0){
            where = [NSString stringWithFormat:@"WHERE %@ = %@ ",field.fieldName,[field getSQLValueStr]];
        }
        else{
            where = [where stringByAppendingString:@"AND"];
            where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ = %@ ",field.fieldName,[field getSQLValueStr]]];
        }
    }
    
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM %@ %@",self.tableName,where];
    
    return (char *)[sql UTF8String];

}

-(char *)getLoadSql
{
    NSString *where =@"";
    
    NSEnumerator * keyFieldKeys = [self.keyFields keyEnumerator];
    
    for (NSString *key in keyFieldKeys) {
        RecordField *field = [self.keyFields objectForKey: key];
        
        if(where.length==0){
            where = [NSString stringWithFormat:@" %@ = %@ ",field.fieldName,[field getSQLValueStr]];
        }
        else{
            where = [where stringByAppendingString:@"AND"];
            where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ = %@ ",field.fieldName,[field getSQLValueStr]]];
        }
    }
    
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM %@  WHERE %@",self.tableName,where];
    
    return (char *)[sql UTF8String];
    
}

-(char *)getUpdateSql
{
    NSString *where =@"";
    NSString *values =@"";
    
    NSEnumerator * keyFieldKeys = [self.keyFields keyEnumerator];
    
    for (NSString *key in keyFieldKeys) {
        RecordField *field = [self.keyFields objectForKey: key];
        
        if(where.length==0){
            where = [NSString stringWithFormat:@" %@ = %@ ",field.fieldName,[field getSQLValueStr]];
        }
        else{
            where = [where stringByAppendingString:@","];
            where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ = %@ ",field.fieldName,[field getSQLValueStr]]];
        }
    }
    
    NSEnumerator * fieldKeys = [self.fields keyEnumerator];
    
    for (NSString *key in fieldKeys) {
        RecordField *field = [self.fields objectForKey: key];
        
        if(values.length==0){
            values = [NSString stringWithFormat:@" %@ = %@ ",field.fieldName,[field getSQLValueStr]];        }
        else{
            values = [values stringByAppendingString:@","];
            values = [values stringByAppendingString:[NSString stringWithFormat:@" %@ = %@ ",field.fieldName,[field getSQLValueStr]]];
        }
    }
    
    NSString *sql = [NSString stringWithFormat:@"UPDATE  %@ SET %@ WHERE %@",self.tableName,values,where];
    
    return (char *)[sql UTF8String];

}

-(char *)getUpdateFieldSql:(NSString *)fieldName
{
    NSString *where =@"";
    NSString *values =@"";
    
    NSEnumerator * keyFieldKeys = [self.keyFields keyEnumerator];
    
    for (NSString *key in keyFieldKeys) {
        RecordField *field = [self.keyFields objectForKey: key];
        
        if(where.length==0){
            where = [NSString stringWithFormat:@" %@ = %@ ",field.fieldName,[field getSQLValueStr]];
        }
        else{
            where = [where stringByAppendingString:@","];
            where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ = %@ ",field.fieldName,[field getSQLValueStr]]];
        }
    }
    
    values = [NSString stringWithFormat:@" %@ = %@ ",fieldName,[[self.fields objectForKey: fieldName] getSQLValueStr]];
    
    NSString *sql = [NSString stringWithFormat:@"UPDATE  %@ SET %@ WHERE %@",self.tableName,values,where];
    
    return (char *)[sql UTF8String];
    
}

-(void) removeField:(NSString *)key
{
    [self.fields removeObjectForKey:key];
}
-(void) addField:(NSString *)key value:(id) value
{
    if([[self.fields allKeys] containsObject:key]){
        RecordField *field = [self.fields objectForKey: key];
        field.value = value;
        return ;
    }

    RecordField * field = [[RecordField alloc] initWithName:key];
    field.value = value;
    [self.fields setObject:field forKey:key];
}
-(void) addField:(NSString *)key value:(id) value type:(enum DataType ) type
{
    if([[self.fields allKeys] containsObject:key]){
        RecordField *field = [self.fields objectForKey: key];
        field.value = value;
        return ;
    }
    RecordField * field = [[RecordField alloc] initWithName:key type:type];
    field.value = value;
    [self.fields setObject:field forKey:key];
}

-(void) addKeyField:(NSString *)key value:(id) value
{
    if([[self.keyFields allKeys] containsObject:key]){
        RecordField *field = [self.keyFields objectForKey: key];
        field.value = value;
        return ;
    }
    RecordField * field = [[RecordField alloc] initWithName:key];
    field.value = value;
    [self.keyFields setObject:field forKey:key];
}
-(void) addKeyField:(NSString *)key value:(id) value type:(enum DataType ) type
{
    if([[self.keyFields allKeys] containsObject:key]){
        RecordField *field = [self.keyFields objectForKey: key];
        field.value = value;
        return ;
    }
    RecordField * field = [[RecordField alloc] initWithName:key type:type];
    field.value = value;
    [self.keyFields setObject:field forKey:key];
}

-(NSString *)getFieldValueByName:(NSString *) fieldName
{
    if([[self.keyFields allKeys] containsObject:fieldName]){
        RecordField *field = [self.keyFields objectForKey: fieldName];  
        return [field getValueStr];
    }else if([[self.fields allKeys] containsObject:fieldName]){
        RecordField *field = [self.fields objectForKey: fieldName];
        return [field getValueStr];
    }
    
    return @"";
}

-(NSData *)getFieldDataByName:(NSString *) fieldName
{
    if([[self.keyFields allKeys] containsObject:fieldName]){
        RecordField *field = [self.keyFields objectForKey: fieldName];
        return [field getValueData];
    }else if([[self.fields allKeys] containsObject:fieldName]){
        RecordField *field = [self.fields objectForKey: fieldName];
        return [field getValueData];
    }
    
    return nil;
}
@end
